﻿using Microsoft.AspNetCore.Authorization;
using Simple.Repository.Enum;
using Simple.Services.System;

namespace Simple.WebApi.Controllers.System
{
    [Route("api/MRegister/[action]")]
    [ApiController]
    [Authorize]
    public class MedicationRegisterController : ControllerBase
    {
        private readonly MedicationRegisterService _medicationRegisterService;
        public MedicationRegisterController(MedicationRegisterService medicationRegisterService)
        {
            _medicationRegisterService = medicationRegisterService;
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<AppResult> Page([FromQuery] MedicationRegisterPageInput model)
        {
            var data = await _medicationRegisterService.GetPage(model);
            return AppResult.Status200OK(data: data);
        }

        /// <summary>
        /// 家长端查询
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<AppResult> ParentPage([FromQuery] MedicationRegisterPageInput model)
        {
            var data = await _medicationRegisterService.GetPage(model, true);
            return AppResult.Status200OK(data: data);
        }

        /// <summary>
        /// 详情
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<AppResult> Get([FromQuery] IdInputModel model)
        {
            var data = await _medicationRegisterService.Get(model.Id);
            return AppResult.Status200OK(data: data);
        }

        /// <summary>
        /// 登记
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<AppResult> Register([FromBody] RegisterInput model)
        {
            await _medicationRegisterService.Register(model);
            return AppResult.Status200OK("登记成功");
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<AppResult> Update([FromBody] RegisterInput model)
        {
            await _medicationRegisterService.Update(model);
            return AppResult.Status200OK("修改成功");
        }

        /// <summary>
        /// 确认
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<AppResult> Confirmed([FromBody] IdInputModel model)
        {
            await _medicationRegisterService.Action(model.Id, EnumMedicationRegisterState.Confirmed, String.Empty);
            return AppResult.Status200OK("确认成功");
        }

        /// <summary>
        /// 撤回
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<AppResult> Rescind([FromBody] IdInputModel model)
        {
            await _medicationRegisterService.Action(model.Id, EnumMedicationRegisterState.Rescinded, String.Empty);
            return AppResult.Status200OK("撤回成功");
        }

        /// <summary>
        /// 驳回
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<AppResult> Reject([FromBody] RejectInput model)
        {
            await _medicationRegisterService.Action(model.Id, EnumMedicationRegisterState.Reject, model.RejectReason);
            return AppResult.Status200OK("驳回成功");
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="models"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<AppResult> Delete([FromBody] List<IdInputModel> models)
        {
            await _medicationRegisterService.DeleteAsync(models.Select(m => m.Id));
            return AppResult.Status200OK("删除成功");
        }
    }
}
